home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / user-setup / user-setup-apply < prev    next >
Encoding:
Text File  |  2009-04-16  |  7.3 KB  |  257 lines

  1. #! /bin/sh
  2. set -e
  3.  
  4. . /usr/share/debconf/confmodule
  5.  
  6. if [ "$1" ]; then
  7.     export LANG=C # avoid locale errors from perl
  8.     ROOT="$1"
  9.     chroot=chroot
  10.     log='log-output -t user-setup'
  11. else
  12.     ROOT=
  13.     chroot=
  14.     log=
  15. fi
  16.  
  17. . /usr/lib/user-setup/functions.sh
  18.  
  19. # Set a password, via chpasswd.
  20. # Use a heredoc rather than echo, to avoid the password
  21. # showing in the process table. (However, this is normally
  22. # only called when first installing the system, when root has no
  23. # password at all, so that should be an unnecessary precaution).
  24. #
  25. # Pass in four arguments: the user, the password, 'true' if the
  26. # password has been pre-crypted (by preseeding), and a 'true' if
  27. # the home directory is encrypted
  28. setpassword () {
  29.     local USER PASSWD OPTS
  30.     USER="$1"
  31.     PASSWD="$2"
  32.     if [ "$3" = true ]; then
  33.         OPTS=-e
  34.     fi
  35.     $chroot $ROOT chpasswd $OPTS <<EOF
  36. $USER:$PASSWD
  37. EOF
  38.     if [ "$4" = true ]; then
  39.         local UNWRAPPED_PASSPHRASE_FILE WRAPPED_PASSPHRASE_FILE MOUNT_PASSPHRASE
  40.         UNWRAPPED_PASSPHRASE_FILE=/dev/shm/.ecryptfs-$USER
  41.         WRAPPED_PASSPHRASE_FILE=/home/$USER/.ecryptfs/wrapped-passphrase
  42.         MOUNT_PASSPHRASE=$($chroot $ROOT cat $UNWRAPPED_PASSPHRASE_FILE)
  43.         $chroot $ROOT ecryptfs-wrap-passphrase $WRAPPED_PASSPHRASE_FILE - <<EOF
  44. $MOUNT_PASSPHRASE
  45. $PASSWD
  46. EOF
  47.         $chroot $ROOT rm -f $UNWRAPPED_PASSPHRASE_FILE
  48.         $chroot $ROOT chown $USER:$USER $WRAPPED_PASSPHRASE_FILE
  49.     fi
  50. }
  51.  
  52. # Enable/disable shadow passwords.
  53. db_get passwd/shadow
  54. if [ "$RET" = true ]; then
  55.     $log $chroot $ROOT shadowconfig on
  56. else
  57.     $log $chroot $ROOT shadowconfig off
  58. fi
  59.  
  60. if ! root_password; then
  61.     # Was the root password preseeded encrypted?
  62.     if db_get passwd/root-password-crypted && [ "$RET" ]; then
  63.         # The root password was preseeded encrypted.
  64.         ROOT_PW="$RET"
  65.         PRECRYPTED=true
  66.     else
  67.         db_get passwd/root-password
  68.         ROOT_PW="$RET"
  69.         PRECRYPTED=false
  70.     fi
  71.     # Clear the root password from the database, and set the password.
  72.     db_set passwd/root-password-crypted ''
  73.     db_set passwd/root-password ''
  74.     db_set passwd/root-password-again ''
  75.     if [ "$ROOT_PW" ]; then
  76.         setpassword root "$ROOT_PW" "$PRECRYPTED"
  77.     fi
  78.     ROOT_PW=
  79. else
  80.     # Just in case, clear any preseeded root password from the database
  81.     # anyway.
  82.     db_set passwd/root-password-crypted ''
  83.     db_set passwd/root-password ''
  84.     db_set passwd/root-password-again ''
  85. fi
  86.  
  87. db_get passwd/make-user
  88. if [ "$RET" = true ] && ! is_system_user; then
  89.     if db_get passwd/user-password-crypted && [ "$RET" ]; then
  90.         USER_PW="$RET"
  91.         USER_PW_CRYPTED=true
  92.     else
  93.         db_get passwd/user-password
  94.         USER_PW="$RET"
  95.         USER_PW_CRYPTED=false
  96.     fi
  97.  
  98.     if db_get passwd/user-uid && [ "$RET" ]; then
  99.         if [ -x $ROOT/usr/sbin/adduser ]; then
  100.             UIDOPT="--uid $RET"
  101.         else
  102.             UIDOPT="-u $RET"
  103.         fi
  104.     else
  105.         UIDOPT=
  106.     fi
  107.  
  108.     ENCRYPT_HOME="false"
  109.     ENCRYPT_HOME_OPT=
  110.     db_get user-setup/encrypt-home
  111.     if [ "$RET" = true ]; then
  112.         ENCRYPT_HOME="true"
  113.         ENCRYPT_HOME_OPT="--encrypt-home"
  114.         if type anna-install >/dev/null 2>&1 && [ -d /lib/debian-installer ]; then
  115.             ANNA_QUIET=1 DEBIAN_FRONTEND=none $log anna-install crypto-modules || true
  116.             depmod -a >/dev/null 2>&1 || true
  117.         fi
  118.         for module in aes cbc ecb; do
  119.             modprobe -q "$module" || true
  120.         done
  121.     fi
  122.  
  123.  
  124.     # Add the user to the database, using adduser in noninteractive
  125.     # mode.
  126.     db_get passwd/username
  127.     USER="$RET"
  128.     db_get passwd/user-fullname
  129.  
  130.     HOME_EXISTED=
  131.     if [ -d "$ROOT/home/$USER" ]; then
  132.         HOME_EXISTED=1
  133.         # user-setup-ask shouldn't have allowed this, but for safety:
  134.         ENCRYPT_HOME="false"
  135.         ENCRYPT_HOME_OPT=
  136.     fi
  137.  
  138.     if [ -n "$ENCRYPT_HOME_OPT" ]; then
  139.         $log $chroot $ROOT mount -t sysfs sysfs /sys
  140.         $log $chroot $ROOT mount -t tmpfs tmpfs /dev/shm
  141.     fi
  142.     if [ -x $ROOT/usr/sbin/adduser ]; then
  143.         $log $chroot $ROOT adduser --disabled-password --gecos "$RET" $UIDOPT $ENCRYPT_HOME_OPT "$USER" >/dev/null || true
  144.     else
  145.         $log $chroot $ROOT useradd -c "$RET" -m "$USER" $UIDOPT >/dev/null || true
  146.     fi
  147.  
  148.     # Clear the user password from the database.
  149.     db_set passwd/user-password-crypted ''
  150.     db_set passwd/user-password ''
  151.     db_set passwd/user-password-again ''
  152.     setpassword "$USER" "$USER_PW" "$USER_PW_CRYPTED" "$ENCRYPT_HOME"
  153.     if [ -n "$ENCRYPT_HOME_OPT" ]; then
  154.         $log $chroot $ROOT umount /sys
  155.         $log $chroot $ROOT umount /dev/shm
  156.     fi
  157.  
  158.     if [ "$HOME_EXISTED" ]; then
  159.         # The user's home directory already existed before we called
  160.         # adduser. This often means that a mount point under
  161.         # /home/$USER was selected in (and thus created by) partman,
  162.         # and the home directory may have ended up owned by root.
  163.         $log $chroot $ROOT chown "$USER:$USER" "/home/$USER" >/dev/null || true
  164.     fi
  165.  
  166.     if [ -n "$USER" ]; then
  167.         for group in lpadmin sambashare; do
  168.             $log $chroot $ROOT addgroup --system $group >/dev/null 2>&1 || true
  169.         done
  170.         if type archdetect >/dev/null 2>&1; then
  171.             SUBARCH="$(archdetect)"
  172.             case $SUBARCH in
  173.                 powerpc/ps3|powerpc/cell)
  174.                     $log $chroot $ROOT addgroup --system spu >/dev/null 2>&1 || true
  175.                     ;;
  176.             esac
  177.         fi
  178.         db_get passwd/user-default-groups
  179.         for group in $RET; do
  180.             $log $chroot $ROOT adduser "$USER" $group >/dev/null 2>&1 || true
  181.         done
  182.  
  183.          # Configure desktop auto-login if instructed by preseeding
  184.          db_get passwd/auto-login
  185.          if [ "$RET" = true ]; then
  186.             db_get passwd/auto-login-backup
  187.             BACKUP="${RET:+.$RET}"
  188.  
  189.             # chroot needed to handle symlinks correctly
  190.             if $chroot $ROOT [ -f /etc/gdm/gdm-cdd.conf ]; then
  191.                 GDMCONF=/etc/gdm/gdm-cdd.conf
  192.             else
  193.                 GDMCONF=/etc/gdm/gdm.conf
  194.             fi
  195.      
  196.             # chroot needed to handle symlinks correctly
  197.             if $chroot $ROOT [ -f "$GDMCONF" ]; then
  198.                 # Configure GDM autologin
  199.                 $log $chroot $ROOT sed -i$BACKUP \
  200.                     -e "s/^AutomaticLoginEnable=.*\$/AutomaticLoginEnable=true/" \
  201.                     -e "s/^AutomaticLogin=.*\$/AutomaticLogin=$USER/" \
  202.                     -e "s/^TimedLoginEnable=.*\$/TimedLoginEnable=true/" \
  203.                     -e "s/^TimedLogin=.*\$/TimedLogin=$USER/" \
  204.                     -e "s/^TimedLoginDelay=.*\$/TimedLoginDelay=10/" \
  205.                     "$GDMCONF"
  206.             fi
  207.      
  208.             if $chroot $ROOT [ -f /etc/kde4/kdm/kdmrc ]; then
  209.                 # Configure KDM autologin
  210.                 $log $chroot $ROOT sed -i$BACKUP -r \
  211.                     -e "s/^#?AutoLoginEnable=.*\$/AutoLoginEnable=true/" \
  212.                     -e "s/^#?AutoLoginUser=.*\$/AutoLoginUser=$USER/" \
  213.                     -e "s/^#?AutoReLogin=.*\$/AutoReLogin=true/" \
  214.                     /etc/kde4/kdm/kdmrc
  215.             fi
  216.         fi
  217.     fi
  218.  
  219.     db_get passwd/root-login
  220.     if [ "$RET" = false ] && [ -n "$USER" ]; then
  221.         # Ensure sudo is installed, and set up the user to be able
  222.         # to use it.
  223.         if [ ! -e $ROOT/etc/sudoers ]; then
  224.             # try to work in d-i and out; it's better to
  225.             # use apt-install in d-i
  226.             apt-install sudo 2>/dev/null || $log $chroot $ROOT apt-get -q -y install sudo || true
  227.         fi
  228.         if [ -e $ROOT/etc/sudoers ]; then
  229.             $log $chroot $ROOT addgroup --system admin >/dev/null 2>&1 || true
  230.             $log $chroot $ROOT adduser "$USER" admin >/dev/null 2>&1 || true
  231.             cat <<EOF >>$ROOT/etc/sudoers
  232.  
  233. # Members of the admin group may gain root privileges
  234. %admin ALL=(ALL) ALL
  235. EOF
  236.         else
  237.             # sudo failed to install, system won't be usable
  238.             exit 1
  239.         fi
  240.         # Configure gksu to use sudo, via an alternative, if it's
  241.         # installed and the alternative is registered.
  242.         if $chroot $ROOT update-alternatives --display libgksu-gconf-defaults >/dev/null 2>&1; then
  243.             $log $chroot $ROOT update-alternatives --set libgksu-gconf-defaults /usr/share/libgksu/debian/gconf-defaults.libgksu-sudo
  244.             $log $chroot $ROOT update-gconf-defaults || true
  245.         fi
  246.     fi
  247.  
  248. else
  249.     # Just in case, clear any preseeded user password from the database
  250.     # anyway.
  251.     db_set passwd/user-password-crypted ''
  252.     db_set passwd/user-password ''
  253.     db_set passwd/user-password-again ''
  254. fi
  255.  
  256. exit 0
  257.